﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Xml;
using StuMana.Services;

namespace StuMana.UI
{
    public partial class InputInfoCafeteria : Office2007Form
    {
        public InputInfoCafeteria()
        {
            InitializeComponent();
        }

        public int level = 0;
        bool flag = false;

        private Font fontForAll = null;
        private StyleManager managerStyle = new DevComponents.DotNetBar.StyleManager();
        public void ReadSetting()
        {
            XmlReader reader = XmlReader.Create("Settings.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                && reader.Name == "font")
                {
                    string name = reader.GetAttribute(0).ToString();
                    string style = reader.GetAttribute(1).ToString();
                    float size = float.Parse(reader.GetAttribute(2).ToString());
                    if (style == "Bold")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (style == "Italic")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (style == "Regular")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (style == "Strikeout")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (style == "Underline")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "managerstyle" && reader.NodeType == XmlNodeType.Element)
                        {
                            reader.Read();
                            string stylemana = reader.Value.ToString();

                            if (stylemana == "Office2007Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Blue;
                            if (stylemana == "Office2007Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Silver;
                            if (stylemana == "Office2007Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Black;
                            if (stylemana == "Windows7Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Windows7Blue;
                            if (stylemana == "Office2007VistaGlass")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007VistaGlass;
                            if (stylemana == "Office2010Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Silver;
                            if (stylemana == "Office2010Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Blue;
                            if (stylemana == "Office2010Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Black;
                        }
                    }
                }
            }
            reader.Close();
        }

        public void ConfigSetting()
        {
            ReadSetting();
            for (int i = 0; i < Controls[0].Controls.Count; i++)
            {
                for (int j = 0; j < Controls[0].Controls[i].Controls.Count; j++)
                {
                    if (Controls[0].Controls[i].Controls[j] is LabelX)
                    {
                        Controls[0].Controls[i].Controls[j].Font = fontForAll;
                    }
                }
            }
            styleManager1 = managerStyle;
        }

        public void GridvieeResize()
        {
            if (dgvXDanhSach.Columns.Count > 0)
            {
                int w = dgvXDanhSach.Size.Width;
                int widthC = ((int)w / 4);
                dgvXDanhSach.Columns[0].Width = widthC;
                dgvXDanhSach.Columns[1].Width = widthC;
                dgvXDanhSach.Columns[2].Width = widthC;
                dgvXDanhSach.Columns[3].Width = widthC;
            }
        }
  
        public bool CheckInputType()
        {
            bool kq = true;
            if (txtTypeName.Text == "")
            {
                MessageBoxEx.Show(" Type Name is blank ", "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTypeName.Focus();
                return false;
            }
            return kq;
        }

        public bool CheckInputItem()
        {
            bool kq = true;
            if (txtName.Text == "")
            {
                MessageBoxEx.Show(" Item Name is blank ", "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }
            int price = 0;
            if (txtPrice.Text == "")
            {
                MessageBoxEx.Show(" Item Price is blank ", "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice.Focus();
                return false;
            }
            if (int.TryParse(txtPrice.Text, out price) == false)
            {
                MessageBoxEx.Show(" Item Price is number (int) ", "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice.Focus();
                return false;
            }

            return kq;
        }

        private void btnAddType_Click(object sender, EventArgs e)
        {
            if (CheckInputType())
            {
                string name = txtTypeName.Text;
                AddFoodType(name);
                LoadFoodTypeData();
                txtTypeName.Text = "";
                foreach (Office2007Form frm in Application.OpenForms)
                {
                    if (frm is UI.Cafeteria)
                    {
                        Cafeteria obj = (Cafeteria)frm;
                        obj.LoadFoodTypeData();
                        return;
                    }
                }
            }
        }

        private void btnDeleteType_Click(object sender, EventArgs e)
        {
            DialogResult dlg = MessageBoxEx.Show("Do you want to  delete type : " + cmbExAllType.Text + " ? ", "Warning ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dlg == DialogResult.Yes)
            {
                int id = int.Parse(cmbExAllType.SelectedValue.ToString());
                DeleteFoodType(id);
                LoadFoodTypeData();
                LoadAllFoodData();
                foreach (Office2007Form frm in Application.OpenForms)
                {
                    if (frm is UI.Cafeteria)
                    {
                        Cafeteria obj = (Cafeteria)frm;
                        obj.LoadFoodTypeData();
                        return;
                    }
                }
            }  
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (CheckInputItem())
            {
                string name = txtName.Text;
                int price = int.Parse(txtPrice.Text);
                int type = int.Parse(cmbExType.SelectedValue.ToString());
                AddFoodData(name, price, type);
                LoadAllFoodData();

                txtName.Text = "";
                txtPrice.Text = "";

                foreach (Office2007Form frm in Application.OpenForms)
                {
                    if (frm is UI.Cafeteria)
                    {
                        Cafeteria obj = (Cafeteria)frm;
                        int loai = int.Parse(obj.cmbFoodType.SelectedValue.ToString());
                        obj.LoadFoodData(loai);
                        return;
                    }
                }
            }
            GridvieeResize();
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (dgvXDanhSach.SelectedRows.Count > 0)
            {
                DialogResult dlg = MessageBoxEx.Show("Do you want to  delete item : " + dgvXDanhSach.SelectedRows[0].Cells[1].Value.ToString() + " ? ", "Warning ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dlg == DialogResult.Yes)
                {
                    int id = int.Parse(dgvXDanhSach.SelectedRows[0].Cells[0].Value.ToString());
                    DeleteFoodData(id);
                    LoadAllFoodData();
                    foreach (Office2007Form frm in Application.OpenForms)
                    {
                        if (frm is UI.Cafeteria)
                        {
                            Cafeteria obj = (Cafeteria)frm;
                            int loai = int.Parse(obj.cmbFoodType.SelectedValue.ToString());
                            obj.LoadFoodData(loai);
                            return;
                        }
                    }
                }
            }
            GridvieeResize();
        }

        private void LoadFoodTypeData()
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "SELECT *  FROM food_type  ";
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                cmbExType.DataSource = ds.Tables[0].DefaultView;
                cmbExType.ValueMember = "ID";
                cmbExType.DisplayMember = "Name";

                DataTable dt = ds.Tables[0];
                cmbExAllType.DataSource = dt;
                cmbExAllType.ValueMember = "ID";
                cmbExAllType.DisplayMember = "Name";
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        public void AddFoodType(string name)
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "INSERT INTO Food_Type(Name)  VALUES(N'" + name.ToString() + "')";
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        public void AddFoodData(string name, int Price, int Type)
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "INSERT INTO Food(Name,Price,Type)  VALUES(N'" + name.ToString() + "'," + Price.ToString() + "," + Type.ToString() + ")";
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        public void DeleteFoodType(int id)
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "Delete From Food_Type Where ID =" + id.ToString();
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        public void DeleteFoodData(int id)
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "Delete From Food Where ID =" + id.ToString();
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        private void LoadAllFoodData()
        {
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "SELECT f.ID, f.Name, f.Price, t.Name as Type  FROM food f , food_type t Where f.Type = t.ID ";
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dgvXDanhSach.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }
        }

        private void dgvXDanhSach_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvXDanhSach.SelectedRows.Count > 0)
            {
                txtName.Text = dgvXDanhSach.SelectedRows[0].Cells[1].Value.ToString();
                txtPrice.Text = dgvXDanhSach.SelectedRows[0].Cells[2].Value.ToString();
                cmbExType.Text = dgvXDanhSach.SelectedRows[0].Cells[3].Value.ToString();
            }
        }
      
        private void InputInfoCafeteria_Resize(object sender, EventArgs e)
        {
            //if (flag == true)
                GridvieeResize();
        }
        
        private void InputInfoCafeteria_Load(object sender, EventArgs e)
        {
            LoadFoodTypeData();
            LoadAllFoodData();
            GridvieeResize();
            ConfigSetting();
            flag = true;
        }

        private void InputInfoCafeteria_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
            Application.ExitThread();
            Application.Exit();
        }

        private void InputInfoCafeteria_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
            Application.ExitThread();
            Application.Exit();
        }
    }
}
